const depsMap = new Map();

function track(key) {
    let dep = depsMap.get(key);
    if (!dep) {
        depsMap.set(key, (dep = new Set()))
    }
    dep.add(effect);
}

function trigger(key) {
    const dep = depsMap.get(key)
    if (dep) {
        dep.forEach(effect => {
            effect()
        })
    }
}

let product = { price: 5, quantity: 2 }
let total = 0

const effect = () => { total = product.price * product.quantity }

track('quantity')
effect()

console.log('total:', total)
product.quantity = 3
console.log('total:', total)
trigger('quantity')
console.log('total:', total)
